<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    Core
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: list.tpl 9568 2011-12-22 21:04:12Z john $
 * @author     John
 */
?>
<?php 
	//only using by lightbox
	$type = 'light';
?>
<script type="text/javascript">
  var CommentLikesTooltips;
 
  en4.core.runonce.add(function() {
	 en4.ialbum.isOtherClick = false;
    // Scroll to comment
    if( window.location.hash != '' ) {
      var hel = $(window.location.hash);
      if( hel ) {
        window.scrollTo(hel);
      }
    }
    // Add hover event to get likes
    $$('.comments_comment_likes').addEvent('mouseover', function(event) {
      var el = $(event.target);
      if( !el.retrieve('tip-loaded', false) ) {
        el.store('tip-loaded', true);
        el.store('tip:title', '<?php echo $this->translate('Loading...') ?>');
        el.store('tip:text', '');
        var id = el.get('id').match(/\d+/)[0];
        // Load the likes
        var url = '<?php echo $this->url(array('module' => 'ialbum', 'controller' => 'comment', 'action' => 'get-likes'), 'default', true) ?>';
        var req = new Request.JSON({
          url : url,
          data : {
            format : 'json',
            type : 'core_comment',
            id : id
            //type : '<?php //echo $this->subject()->getType() ?>',
            //id : '<?php //echo $this->subject()->getIdentity() ?>',
            //comment_id : id
          },
          onComplete : function(responseJSON) {
            el.store('tip:title', responseJSON.body);
            el.store('tip:text', '');
            CommentLikesTooltips.elementEnter(event, el); // Force it to update the text
          }
        });
        req.send();
      }
    });
    // Add tooltips
    CommentLikesTooltips = new Tips($$('.comments_comment_likes'), {
      fixed : true,
      className : 'comments_comment_likes_tips',
      offset : {
        'x' : 48,
        'y' : 16
      }
    });
    // Enable links
    $$('.comments_body').enableLinks();

    // for two link under big images
     <?php if( $this->subject()->likes()->isLike($this->viewer()) ): ?>
    	if($('likeAlbum')) $('likeAlbum').setStyle('display', 'none');
    	if($('unLikeAlbum')) $('unLikeAlbum').setStyle('display', '');
    <?php else:?>
	    if($('unLikeAlbum')) $('unLikeAlbum').setStyle('display', 'none');
	    if($('likeAlbum')) $('likeAlbum').setStyle('display', '');
    <?php endif;?>
	//enable buttonClose when mouseover
    $$('.comments  li').each(function(el){
        $(el).addEvents({
			mouseover: function(){
				if($(this).getElement('i.closeButton'))
					$(this).getElement('i.closeButton').setStyle('display', 'block');
			},
			mouseout: function(){
				if($(this).getElement('i.closeButton'))
					$(this).getElement('i.closeButton').setStyle('display', 'none');
			}
        });			
     });
    //handle textarea commentBodyOver
    $('commentBodyOver_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>').addEvents({
		'focus': function() {
			if (this.value.contains('<?php echo $this->translate('Write a comment...')?>')) this.value = '';
			$('des_comment_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>').show();
		},
		'keypress': function(event) {
			if( event.key == 'enter' && event.shift ){
				//do nothing
			}else if(event.key == 'enter'){
				event.stop();
				if(this.value.trim() == '') return false;
				$('commentFormOver_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>').fireEvent('submit');
			}		 	
		},
		'blur': function(){
			if (this.value == '') this.value = '<?php echo $this->translate('Write a comment...')?>';
			$('des_comment_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>').hide();
		} 
    });
  });
</script>

<?php $this->headTranslate(array(
  'Are you sure you want to delete this?',
)); ?>

<?php if( !$this->page ): ?>
<div class='comments ialbum' id="comments_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>">

<?php endif; ?>
  <div class='comments_options'>
    <?php /*?><span><?php echo $this->translate(array('%s comment', '%s comments', $this->comments->getTotalItemCount()), $this->locale()->toNumber($this->comments->getTotalItemCount())) ?></span><?php */?>

    <?php if( $this->viewer()->getIdentity() && $this->canComment ): ?>
		      <?php if( $this->subject()->likes()->isLike($this->viewer()) ): ?>
		        <a href="javascript:void(0);" onclick="en4.ialbum.comments.unlike('<?php echo $this->subject()->getType()?>', '<?php echo $this->subject()->getIdentity() ?>')"><?php echo $this->translate('Unlike') ?></a>
		      <?php else: ?>
		        <a href="javascript:void(0);" onclick="en4.ialbum.comments.like('<?php echo $this->subject()->getType()?>', '<?php echo $this->subject()->getIdentity() ?>')"><?php echo $this->translate('Like') ?></a>
		      <?php endif; ?>
		      
		      <?php if( isset($this->form) ): ?>
		      - <a href='javascript:void(0);' onclick="$('commentBodyOver_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>').focus();"><?php echo $this->translate('Comment') ?></a>
		   	 <?php endif; ?>
		   	 
		   	  - <?php echo $this->htmlLink(Array('module'=> 'activity', 'controller' => 'index', 'action' => 'share', 'route' => 'default', 'type' => 'album_photo', 'id' => $this->subject()->getIdentity(), 'format' => 'smoothbox'), $this->translate("Share"), array('class' => 'smoothbox')); ?>
		   	 <?php if($this->canEditPhoto && $this->subject()->getType() != 'album' && $this->photo_id != 0):?>
			  - <a id="aEdit" class="<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>" href="javascript: void(0);" onclick="editPhoto_view(this, true);$('titlePhotoValue').focus();"><?php echo $this->translate('Edit')?></a>			
			<?php endif?>
			- <?php echo $this->timestamp($this->subject()->modified_date)?>
	<?php endif; ?>
  </div>
  <ul>
    <?php if( $this->viewer()->getIdentity() && $this->canComment ): ?>
    	<li class="uiListVerticalItemBorder"><i></i>
    <?php endif;?>
	</li>
    <?php if( $this->likes->getTotalItemCount() > 0 ): // LIKES ------------- ?>
      <li class="listComment">
        <?php if( $this->viewAllLikes || $this->likes->getTotalItemCount() <= 3 ): ?>
          <?php $this->likes->setItemCountPerPage($this->likes->getTotalItemCount()) ?>
          <div> </div>
          <div class="comments_likes">
          	<i></i>
            <?php echo $this->translate(array('%s likes this', '%s like this', $this->likes->getTotalItemCount()), $this->fluentList($this->subject()->likes()->getAllLikesUsers())) ?>.
          </div>
        <?php else: ?>
          <div> </div>
          <div class="comments_likes">
            <?php echo $this->htmlLink('javascript:void(0);', 
                          $this->translate(array('%s person likes this', '%s people like this', $this->likes->getTotalItemCount()), $this->locale()->toNumber($this->likes->getTotalItemCount())),
                          array('onclick' => 'en4.ialbum.comments.showLikes("'.$this->subject()->getType().'", "'.$this->subject()->getIdentity().'");')
                      ); ?>.
          </div>
        <?php endif; ?>
    <?php endif; ?>

    <?php if( $this->comments->getTotalItemCount() > 0 ): // COMMENTS ------- ?>

      <?php if( $this->page && $this->comments->getCurrentPageNumber() > 1 ): ?>
        <li class="listComment">
          <div> </div>
          <div class="comments_viewall">
            <?php echo $this->htmlLink('javascript:void(0);', $this->translate('View previous comments'), array(
              'onclick' => 'en4.ialbum.comments.loadComments("'.$this->subject()->getType().'", "'.$this->subject()->getIdentity().'", "'.($this->page - 1).'")'
            )) ?>
          </div>
        </li>
      <?php endif; ?>

      <?php if( !$this->page && $this->comments->getCurrentPageNumber() < $this->comments->count() ): ?>
        <li class="listComment">
          <div> </div>
          <div class="comments_viewall">
            <?php echo $this->htmlLink('javascript:void(0);', $this->translate('View more comments'), array(
              'onclick' => 'en4.ialbum.comments.loadComments("'.$this->subject()->getType().'", "'.$this->subject()->getIdentity().'", "'.($this->comments->getCurrentPageNumber()).'")'
            )) ?>
          </div>
        </li>
      <?php endif; ?>

      <?php // Iterate over the comments backwards (or forwards!)
      $comments = $this->comments->getIterator();
      if( $this->page ):
        $i = 0;
        $l = count($comments) - 1;
        $d = 1;
        $e = $l + 1;
      else:
        $i = count($comments) - 1;
        $l = count($comments);
        $d = -1;
        $e = -1;
      endif;
      for( ; $i != $e; $i += $d ):
        $comment = $comments[$i];
        $poster = $this->item($comment->poster_type, $comment->poster_id);
        $canDelete = ( $this->canDelete || $poster->isSelf($this->viewer()) );
        ?>
        <li class="listComment" id="comment-<?php echo $comment->comment_id ?>">
          <div class="comments_author_photo">
            <?php echo $this->htmlLink($poster->getHref(),
              $this->itemPhoto($poster, 'thumb.icon', $poster->getTitle())
            ) ?>
          </div>
          <div class="comments_info">
            <span class='comments_author'>
              <?php echo $this->htmlLink($poster->getHref(), $poster->getTitle()); ?>
            </span>
            <span class="comments_body">
              <?php echo $this->viewMore($comment->body) ?>
            </span>
            <div class="comments_date">
              <?php echo $this->timestamp($comment->creation_date); ?>
              <?php /*if( $canDelete ): ?>
                -
                <a href="javascript:void(0);" onclick="en4.ialbum.comments.deleteComment('<?php echo $this->subject()->getType()?>', '<?php echo $this->subject()->getIdentity() ?>', '<?php echo $comment->comment_id ?>')">
                  <?php echo $this->translate('delete') ?>
                </a>
              <?php endif; */?>
              <?php if( $this->canComment ):
                $isLiked = $comment->likes()->isLike($this->viewer());
                ?>
                -
                <?php if( !$isLiked ): ?>
                  <a href="javascript:void(0)" onclick="en4.ialbum.comments.like(<?php echo sprintf("'%s', %d, %d", $this->subject()->getType(), $this->subject()->getIdentity(), $comment->getIdentity()) ?>)">
                    <?php echo $this->translate('Like') ?>
                  </a>
                <?php else: ?>
                  <a href="javascript:void(0)" onclick="en4.ialbum.comments.unlike(<?php echo sprintf("'%s', %d, %d", $this->subject()->getType(), $this->subject()->getIdentity(), $comment->getIdentity()) ?>)">
                    <?php echo $this->translate('Unlike') ?>
                  </a>
                <?php endif ?>
              <?php endif ?>
              <?php if( $comment->likes()->getLikeCount() > 0 ): ?>
                -
                <a href="javascript:void(0);" id="comments_comment_likes_<?php echo $this->subject()->getType()?>_<?php echo $comment->comment_id ?>" class="comments_comment_likes" title="<?php echo $this->translate('Loading...') ?>">
                  <i class="icon_like_like"></i><?php echo $this->translate(array('%s', '%s', $comment->likes()->getLikeCount()), $this->locale()->toNumber($comment->likes()->getLikeCount())) ?>
                </a>
              <?php endif ?>
            </div>
            <?php /*
            <div class="comments_date">
              <?php echo $this->timestamp($comment->creation_date); ?>
              <?php if( $comment->likes()->getLikeCount() > 0 ): ?>
                -
                <a href="javascript:void(0);" id="comments_comment_likes_<?php echo $comment->comment_id ?>" class="comments_comment_likes" title="<?php echo $this->translate('Loading...') ?>">
                  <?php echo $this->translate(array('%s likes this', '%s like this', $comment->likes()->getLikeCount()), $this->locale()->toNumber($comment->likes()->getLikeCount())) ?>
                </a>
              <?php endif ?>
            </div>
            <div class="comments_comment_options">
              <?php if( $canDelete && $this->canComment ): ?>
                -
              <?php endif ?>
            </div>
             *
             */ ?>
          </div>
          <!-- close button -->
          <?php if( $canDelete ): ?>
         	 <i class="closeButton"  onclick="en4.ialbum.comments.deleteComment('<?php echo $this->subject()->getType()?>', '<?php echo $this->subject()->getIdentity() ?>', '<?php echo $comment->comment_id ?>')"></i>
          <?php endif; ?>
        </li>
      <?php endfor; ?>

      <?php if( $this->page && $this->comments->getCurrentPageNumber() < $this->comments->count() ): ?>
        <li class="listComment">
          <div> </div>
          <div class="comments_viewall">
            <?php echo $this->htmlLink('javascript:void(0);', $this->translate('View later comments'), array(
              'onclick' => 'en4.ialbum.comments.loadComments("'.$this->subject()->getType().'", "'.$this->subject()->getIdentity().'", "'.($this->page + 1).'")'
            )) ?>
          </div>
        </li>
      <?php endif; ?>

    <?php endif; ?>
    <?php if( $this->viewer()->getIdentity() && $this->canComment ): ?>
	    <li class="doComment" id="doComment_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>">
	    	<div class="mentionsAddComment">
	    		<?php echo $this->itemPhoto($this->viewer(), 'thumb.icon', $this->viewer()->getTitle());?>
	    		<form id="commentFormOver_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>" name="commentFormOver" method="post" enctype="application/x-www-form-urlencoded">
		    		<div class="commentArea">
		    			<textarea name="body" id="commentBodyOver_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>" class="uiTextareaAutogrow"><?php echo $this->translate('Write a comment...')?></textarea>
		    			<input type="hidden" id="type_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>" value="<?php echo $this->subject()->getType()?>" name="type" class="<?php echo $type?>">
		    			<input type="hidden" id="identity_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>" value="<?php echo $this->subject()->getIdentity()?>" name="identity">		    					    		 
		    		</div>
	    		 </form>
	    		 <span class="des_comment" id="des_comment_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>"><?php echo $this->translate('Press Shift+Enter to break line.')?></span>
	    	</div>
	    </li>    	
    <?php endif;?>

  </ul>
  <script type="text/javascript">
    en4.core.runonce.add(function(){
      $('commentBodyOver_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>').autogrow();
      en4.ialbum.comments.attachCreateComment($('commentFormOver_<?php echo $this->subject()->getType()?>_<?php echo $this->subject()->getIdentity()?>'));
    });
  </script>
  <?php if( isset($this->form) ) echo $this->form->setAttribs(array('id' => 'comment-form', 'style' => 'display:none;'))->render() ?>
<?php if( !$this->page ): ?>
</div>
    <?php endif; ?>